
#include "StateEndGame.h"
#include "StateMainMenu.h"
//#include "StateShop.h"
#include "StateShopAnimals.h"
#include "defs.h"
#include "StateManager.h"
#include "LevelManager.h"
#include "ProgressManager.h"
#include "BackgroundManager.h"
#include "ScoreManager.h"
#include "MusicManager.h"

StateEndGame::StateEndGame()
{
}

StateEndGame::~StateEndGame()
{
}

void StateEndGame::loadContent()
{
    m_oBGFade = new RenderableEntity("fade", Vector2(0, 0));
    m_oBGFade->setScale(Vector2(640, 960));
    m_oBGFade->setColor(COLOR_BLACK);
    m_oBGFade->setAlpha(0.0f);

    m_oBG = new RenderableEntity("Results_BG", Vector2(320, 480));
    m_oBG->setCentered();
    m_oBG->setLiving(true);
    
    m_oRibbon = new RenderableEntity("MENU_0009_ribbon", Vector2(320, 820));
    m_oRibbon->setCentered();
    m_oRibbon->setLiving(true);

    m_oResults = new TextEntity("Results", FONT_NAME, 64, Vector2(320, 835), 1, 1);
    m_oResults->setColor(COLOR_PINK_TEXT);
    m_oResults->setShadow(true, COLOR_PINK_DARK);
    m_oResults->setCentered();
    
    m_oBabiesCurrentValue = new TextEntity(" ", FONT_NAME, 40, Vector2(305, 700), 1, 1);
    m_oBabiesCurrentValue->setShadow();
    m_oBabiesCurrentValue->setCenter(Vector2(0.5f, 0.5f));
    m_oBabiesCurrentIcon = new RenderableEntity("icon_diaper", Vector2(440, 695));
    m_oBabiesCurrentIcon->setCenter(Vector2(0.5f, 0.5f));
    m_oBabiesCurrentIcon->setScale(Vector2(0.4f, 0.4f));

    m_oCoinsCurrentValue = new TextEntity(" ", FONT_NAME, 40, Vector2(305, 630), 1, 1);
    m_oCoinsCurrentValue->setShadow();
    m_oCoinsCurrentValue->setCenter(Vector2(0.5f, 0.5f));
    m_oCoinsCurrentIcon = new RenderableEntity("icon_babycoin", Vector2(500, 625));
    m_oCoinsCurrentIcon->setCenter(Vector2(0.5f, 0.5f));
    m_oCoinsCurrentIcon->setScale(Vector2(0.4f, 0.4f));

    m_oTotalsBg = new RenderableEntity("Results_TotalsBG", Vector2(320, 500));
    m_oTotalsBg->setCentered();
    m_oTotalsBg->changeColorWhenClicked(true);
    m_oCoinsTotalValue = new TextEntity(" ", FONT_NAME, 32, Vector2(305, 520), 1, 1);
    m_oCoinsTotalValue->setShadow();
    m_oCoinsTotalValue->setCenter(Vector2(0.5f, 0.5f));
    m_oCoinsTotalIcon = new RenderableEntity("icon_babycoin", Vector2(360, 520));
    m_oCoinsTotalIcon->setCenter(Vector2(0.5f, 0.5f));
    m_oCoinsTotalIcon->setScale(Vector2(0.32f, 0.32f));
    m_oGoToShop = new TextEntity("Go to Shop", FONT_NAME, 32, Vector2(320, 470), 1, 1);
    m_oGoToShop->setShadow();
    m_oGoToShop->setCentered();

    m_oFacebook = new RenderableEntity("facebook", Vector2(210, 370));
    m_oFacebook->changeColorWhenClicked(true);
    m_oFacebook->setCentered();
    m_oFacebook->setScale(Vector2(0.6f, 0.6f));
    m_oFacebook->setRotation(0.2f);
    
    m_oTwitter = new RenderableEntity("twitter", Vector2(430, 370));
    m_oTwitter->changeColorWhenClicked(true);
    m_oTwitter->setCentered();
    m_oTwitter->setScale(Vector2(0.6f, 0.6f));
    m_oTwitter->setRotation(-0.2f);
    
    m_oLeftBg = new RenderableEntity("buttonresults", Vector2(200, 250));
    m_oLeftBg->setScale(Vector2(1.0f, 1.0f));
    m_oLeftBg->setCentered();
    
    m_tMainMenu = new TextEntity("Main Menu", FONT_NAME, 32, Vector2(200, 250), 1, 1);
    m_tMainMenu->setCentered();
    m_tMainMenu->changeColorWhenClicked(true);
    m_tMainMenu->setupButton(Vector2(0.7f, 0.7f));
    
    m_oRightBg = new RenderableEntity("buttonresults", Vector2(440, 250));
    m_oRightBg->setScale(Vector2(1.0f, 1.0f));
    m_oRightBg->setCentered();

    m_tPlayAgain = new TextEntity("Play Again", FONT_NAME, 32, Vector2(440, 250), 1, 1);
    m_tPlayAgain->setCentered();
    m_tPlayAgain->changeColorWhenClicked(true);
    m_tPlayAgain->setupButton(Vector2(0.7f, 0.7f));
    
    
    //Enter
    m_eEndGame_Enter = new TimeLineEffect(false, false);
    m_eEndGame_Enter->addEffect(new FadeEffect(m_oBGFade, 0.1, false, false, 0.0f, 0.6f), 0);
    
    m_eEndGame_Enter->addInEffect(m_tMainMenu);
    m_eEndGame_Enter->addInEffect(m_tPlayAgain);
    m_eEndGame_Enter->addInEffect(m_oBG);
    m_eEndGame_Enter->addInEffect(m_oRibbon);
    m_eEndGame_Enter->addInEffect(m_oResults);
    m_eEndGame_Enter->addInEffect(m_oBabiesCurrentValue);
    m_eEndGame_Enter->addInEffect(m_oBabiesCurrentIcon);
    m_eEndGame_Enter->addInEffect(m_oCoinsCurrentValue);
    m_eEndGame_Enter->addInEffect(m_oCoinsCurrentIcon);
    m_eEndGame_Enter->addInEffect(m_oTotalsBg);
    m_eEndGame_Enter->addInEffect(m_oCoinsTotalValue);
    m_eEndGame_Enter->addInEffect(m_oCoinsTotalIcon);
    m_eEndGame_Enter->addInEffect(m_oGoToShop);
    m_eEndGame_Enter->addInEffect(m_oFacebook);
    m_eEndGame_Enter->addInEffect(m_oTwitter);
    m_eEndGame_Enter->addInEffect(m_oLeftBg);
    m_eEndGame_Enter->addInEffect(m_oRightBg);
    
    //Exit
    m_eEndGame_MainMenu = new TimeLineEffect(false, false);

    //Exit
    m_eEndGame_PlayAgain = new TimeLineEffect(false, false);
    m_eEndGame_PlayAgain->addEffect(new FadeEffect(m_oBGFade, 0.3, false, false, 0.6f, 0.0f), 0);

    m_eEndGame_PlayAgain->addOutEffect(m_tMainMenu);
    m_eEndGame_PlayAgain->addOutEffect(m_tPlayAgain);
    m_eEndGame_PlayAgain->addOutEffect(m_oBG);
    m_eEndGame_PlayAgain->addOutEffect(m_oRibbon);
    m_eEndGame_PlayAgain->addOutEffect(m_oResults);
    m_eEndGame_PlayAgain->addOutEffect(m_oBabiesCurrentValue);
    m_eEndGame_PlayAgain->addOutEffect(m_oBabiesCurrentIcon);
    m_eEndGame_PlayAgain->addOutEffect(m_oCoinsCurrentValue);
    m_eEndGame_PlayAgain->addOutEffect(m_oCoinsCurrentIcon);
    m_eEndGame_PlayAgain->addOutEffect(m_oTotalsBg);
    m_eEndGame_PlayAgain->addOutEffect(m_oCoinsTotalValue);
    m_eEndGame_PlayAgain->addOutEffect(m_oCoinsTotalIcon);
    m_eEndGame_PlayAgain->addOutEffect(m_oGoToShop);
    m_eEndGame_PlayAgain->addOutEffect(m_oFacebook);
    m_eEndGame_PlayAgain->addOutEffect(m_oTwitter);
    m_eEndGame_PlayAgain->addOutEffect(m_oLeftBg);
    m_eEndGame_PlayAgain->addOutEffect(m_oRightBg);
}

void StateEndGame::unloadContent()
{
    delete m_eEndGame_PlayAgain;
    delete m_eEndGame_MainMenu;
    delete m_eEndGame_Enter;
    
    delete m_oBGFade;
    delete m_tMainMenu;
    delete m_tPlayAgain;
}

void StateEndGame::enter()
{
    char buff[30];
    
    sprintf(buff, "You fed %i", LevelManager::instance().getScore());
    m_oBabiesCurrentValue->setText(buff);
    m_oBabiesCurrentIcon->setPosition(Vector2(330 + m_oBabiesCurrentValue->getTextSize().x * 0.5f, 695));
    
    sprintf(buff, "and collected %i", LevelManager::instance().getCoins());
    m_oCoinsCurrentValue->setText(buff);
    m_oCoinsCurrentIcon->setPosition(Vector2(330 + m_oCoinsCurrentValue->getTextSize().x * 0.5f, 625));
    
    sprintf(buff, "%i", ProgressManager::instance().getCoins());
    m_oCoinsTotalValue->setText(buff);
    m_oCoinsTotalIcon->setPosition(Vector2(330 + m_oCoinsTotalValue->getTextSize().x * 0.5f , 515));
    
    m_eState = Entering;
    m_fTimeInState = 0;
    m_eEndGame_Enter->start(true);    
}

void StateEndGame::update(float dt)
{
    m_fTimeInState += dt;
    
    switch(m_eState)
    {
        case Entering:
        {
            if(m_eEndGame_Enter->isFinished())
            {
                m_eState = Showing;
            }
            break;
        }
            
        case Showing:
        {
            m_oBG->update();
            
            m_oRibbon->update();
            m_oResults->setScale(m_oRibbon->getScale());
            
            m_tMainMenu->update();
            m_tPlayAgain->update();
            
            m_oFacebook->update();
            m_oTwitter->update();
            m_oTotalsBg->update();
                        
            if(m_tPlayAgain->isClicked())
            {
                LevelManager::instance().startCountdown();
                m_eState = BackToGame;
                m_fTimeInState = 0;
                m_eEndGame_PlayAgain->start(true);
            }
            else if(m_tMainMenu->isClicked())
            {
                m_eState = ToMainMenu;
                //StateManager::instance().fadeOut();
                m_eEndGame_MainMenu->start(true);
            }
            else if(m_oTotalsBg->isClicked())
            {
                m_eState = ToShop;
                
                StateManager::instance().fadeOut();
                m_eEndGame_MainMenu->start(true);
                
                BackgroundManager::instance().fadeOut();
            }

            break;
        }

        case BackToGame:
        {
            if(m_eEndGame_PlayAgain->isFinished())
            {
                StateManager::instance().popState();
            }

            break;
        }
            
        case ToMainMenu:
            if(m_eEndGame_MainMenu->isFinished())
            {
                BackgroundManager::instance().fadeOut();
                StateManager::instance().setState(StateMainMenu::instance());
                //StateManager::instance().fadeIn();
                
                MusicManager::instance().playMusic();
            }
            break;

        case ToShop:
            if(!StateManager::instance().isFading() && m_eEndGame_MainMenu->isFinished())
            {
//                StateManager::instance().setState(StateShop::instance());
                StateManager::instance().setState(StateShopAnimals::instance());
                StateManager::instance().fadeIn();
                
                MusicManager::instance().playMusic();
            }
            break;
    }
}

void StateEndGame::render()
{
    m_oBGFade->render();
    m_oBG->render();
    
    m_oRibbon->render();
    m_oResults->render();
    
    m_oBabiesCurrentIcon->render();
    m_oBabiesCurrentValue->render();
    m_oCoinsCurrentIcon->render();
    m_oCoinsCurrentValue->render();
    
    m_oTotalsBg->render();
    m_oCoinsTotalIcon->render();
    m_oCoinsTotalValue->render();
    m_oGoToShop->render();
    
    m_oFacebook->render();
    m_oTwitter->render();
    
    m_oLeftBg->render();
    m_oRightBg->render();
    m_tMainMenu->render();
    m_tPlayAgain->render();
}

void StateEndGame::exit()
{
}
